package com.plug.service;

import com.plug.common.jdbc.DBConnectionManager;
import com.plug.common.jdbc.DBConnectionPool;
import com.plug.entity.PlugDatasource;
import com.plug.repository.PlugDatasourceMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/12/19.
 */
@Service("plugDatasourceService")
public class PlugDatasourceServiceImpl implements IPlugDatasourceService{
    @Resource
    private PlugDatasourceMapper plugDatasourceMapper;

    private DBConnectionManager dbConnectionManager = DBConnectionManager.getInstance();
    /**
     * 根据名称查询数据�?
     * @param name 数据库别名
     * @return
     */
    public PlugDatasource findByName(String name) {
        return plugDatasourceMapper.findByName(name);
    }

    /**
     * 新增数据源
     * @param plugDatasource
     * @return
     */
    public int save(PlugDatasource plugDatasource) {
        if(plugDatasource != null) {
            if(plugDatasource.getId() != null) {
                PlugDatasource plugData = plugDatasourceMapper.selectByPrimaryKey(plugDatasource.getId());
                plugDatasource.setId(plugData.getId());
                return plugDatasourceMapper.updateByPrimaryKeySelective(plugDatasource);
            } else {
                return plugDatasourceMapper.insertSelective(plugDatasource);
            }
        } else {
            return -1;
        }
    }

    /**
     * 分页查询数据源
     * @param name
     * @param offset
     * @param limit
     * @return
     */
    public List<PlugDatasource> pages(String name, Integer offset, Integer limit) {
        return plugDatasourceMapper.pages(name, offset, limit);
    }

    /**
     * 查询数据总数
     * @param name
     * @return
     */
    public int findTotal(String name) {

        return plugDatasourceMapper.findTotal(name);
    }

    /**
     * 删除数据源
     * @param id
     * @return
     */
    public int delete(Long id) {
        return plugDatasourceMapper.deleteByPrimaryKey(id);}

    /**
     * 根据ID查询
     * @param id
     * @return PlugDatasource
     */
    public PlugDatasource findByID(Long id) {
        return plugDatasourceMapper.selectByPrimaryKey(id);}


    /**
     * 查询所有
     * @return
     */
    public List<PlugDatasource> findall(){
        return plugDatasourceMapper.findall();
    }

    /**
     * 根据数据库名称查询属下所有表
     * @param plugDatasource
     * @return
     */
    public List<Map<String,Object>> findDataBaseByName(@Param("plugDatasource")PlugDatasource plugDatasource) throws SQLException {

        List<Map<String,Object>> tableList=new ArrayList<>();
        Connection connection=null;
        PreparedStatement ps = null;
        ResultSet rs = null;

            connection = dbConnectionManager.getConnection(DBConnectionPool.createJDBCInfoBean(plugDatasource));
            ps = connection.prepareStatement(" select table_name from information_schema.tables where table_schema= '"+plugDatasource.getDsDesc()+"' and  table_type='base table' ");
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData(); // 取得结果集的元元素
            int colCount = metaData.getColumnCount(); // 取得所有列的个数

            while (rs.next()){
                Map<String, Object> map = new HashMap<String, Object>();
                for (int i = 1; i <= colCount; i++) {
                    Object value = rs.getObject(i);
                    map.put(metaData.getColumnName(i), value);
                }
                tableList.add(map);
            }

            try{
                if(rs != null)
                    rs.close();
                if(ps != null)
                    ps.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
            dbConnectionManager.freeConnection(plugDatasource.getDsName(), connection);

        return tableList;
    }
}
